#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
The function load() will load a stardict format dictionary into
a Python dict with key-value pair 'english_word' : 'french_word1; 
french_word2; french_word3; ...'

Guan Gui (ggui, 312348)
$Rev: 52 $
$LastChangedDate: 2010-03-17 05:03:11 +0000 (Wed, 17 Mar 2010) $
"""

import re
import sys
from pystardict import Dictionary
from nltk import defaultdict

def load(path_to_dict):
    # Although pystardict.Dictionary is a child class of dict, it doesn't
    # implement quite a few important basic method such as keys(), iterkeys()
    # and etc, so we cannot just simply iterate through it.
    raw_dict = Dictionary(path_to_dict);
   
    new_dict = defaultdict(tuple)
    size = float(len(raw_dict))
    count = 0
   
    # This is a workaround to iterate through the keys
    # NB Since the idx stores the offset-size pairs, its keys must be sorted in 
    # order to read the dictionary data linearly and gain the best performance
    for tuple_key in sorted(raw_dict.idx._idx):
        key = ''.join(tuple_key)
        value = raw_dict[key]
        # Convert value to set of French words
        value = re.sub(r'\d\. ?', '', value);
        value = re.split(r', | \n ', value);
        new_dict[key] = value;
        # Show a nice progress report
        count += 1
        print 'Loading dictionary...       %5.2f%%\r' % ((count / size) * 100),
        sys.stdout.flush() # this must be flushed to see the latest result
        
    print
    return new_dict

if __name__ == '__main__':
    # test the conversion and print all entries of a stardict format dictionary
    my_dict = load('stardict-woaifayu-ef-2.4.2/woaifayu-ef')
    for key in sorted(my_dict.keys()):
        print "%s: %s" % (key, '; '.join(my_dict[key]))
    
